#include "Task.hpp"
#include "RingQueue.hpp"
#include<ctime>
#include<cstdlib>
#include<pthread.h>
#include<unistd.h>
void* Consumer(void* _rq)
{
    //RingQueue<int>* rq = reinterpret_cast<RingQueue<int>*>(_rq)
    RingQueue<Task>* rq = reinterpret_cast<RingQueue<Task>*>(_rq);

    while(true)  //消费
    {
        Task t;
        rq->Pop(&t); 
        std::cout<<"消费者处理任务:" ;
        t();
    }

    return nullptr;
}

void* Producter(void* _rq)
{
    //RingQueue<int>* rq = reinterpret_cast<RingQueue<int>*>(_rq);
    RingQueue<Task>* rq = reinterpret_cast<RingQueue<Task>*>(_rq);
    while(true)   // 生产
    {
        int x = rand()%10+1;
        int y = rand()%20+1;
        char op = "+-*/%"[rand()%5];
        Task t1(x,y,op);
        std::cout<<"生产了任务------"<< x << op << y << "=?"<<std::endl;
        rq->Push(t1);
        sleep(1);
    }
    return nullptr;
}


int main()
{
    srand((unsigned int)time(nullptr) ^ pthread_self());     //生产随机数种子

    //RingQueue<int>* rq = new RingQueue<int>();
    RingQueue<Task>* rq = new RingQueue<Task>();
    pthread_t c,p;
    pthread_create(&c,nullptr,Consumer,rq);
    pthread_create(&p,nullptr,Producter,rq);


    pthread_join(c,nullptr);
    pthread_join(p,nullptr);
    return 0;
}
